﻿/**
 ** @作者:	   王桂龙 QQ731513122
 ** @日期:	   2019.12.6
 ** @简介:     一个自定义form控件
 **            实现对clientpanel的子控件继承于IBHXJZoom接口的，非Dock类的控件进行缩放控制    
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BHXJDOTNET.OLD;

namespace BHXJDOTNET.FORM
{
    /// <summary>
    /// 比BHXJForm运行流畅，推荐使用
    /// </summary>
    public partial class BHXJFormN : Form, IBHXJZOOM,IBHXJCONTAINER
    {
        #region 继承于ZOOM接口的属性和方法
        private Point _OriginPoint = new Point(0, 0);
        [Description("原点(慎用,在足够了解BHXJZoom的情况下使用)"), Category("BHXJZoom")]
        /// <summary>
        /// 原点
        /// </summary>
        public Point OriginPoint
        {
            get
            { return this._OriginPoint; }
            set
            {
                if (this._OriginPoint != value)
                {
                    this._OriginPoint = value;
                    if (DesignMode)
                        this.IniLocation = this.BHXJGetIniLocationInDesignMode();
                }
            }
        }
        private bool _AutoOrigin = false;
        [Description("自动更改原点模式(只在设计模式下有用)"), Category("BHXJZoom")]
        /// <summary>
        /// 自动更改原点模式(只在设计模式下有用)
        /// </summary>
        public bool AutoOrigin
        {
            get
            {
                return this._AutoOrigin;
            }
            set
            {
                if (this._AutoOrigin != value)
                {
                    this._AutoOrigin = value;
                    if (DesignMode)
                    {
                        if (this.AutoOrigin)
                        {
                            this.OriginPoint = this.Location;
                        }
                        else
                        {
                            this.OriginPoint = new Point(0, 0);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 初始尺寸
        /// </summary>
        [Browsable(false)]
        public Size IniSize { get; set; }
        /// <summary>
        /// 初始位置
        /// </summary>
        [Browsable(false)]
        public Point IniLocation { get; set; }
        /// <summary>
        /// 初始字体
        /// </summary>
        [Browsable(false)]
        public Font IniFont { get; set; }

        #endregion

        public new AutoScaleMode AutoScaleMode = AutoScaleMode.None;

        #region 内部IZoom控件缩放
        private bool _ZoomFlag = false;
        [Description("是否对子控件启用Zoom功能"), Category("BHXJZoom")]
        /// <summary>
        /// 是否对子控件启用Zoom功能
        /// </summary>
        public bool ZoomFlag
        {
            get { return this._ZoomFlag; }
            set
            {
                if (value != this._ZoomFlag)
                {
                    this._ZoomFlag = value;
                    //重新缩放
                    this.BhxjPanelClient_SizeChanged(this, null);
                }
            }
        }

        private bool _ZoomSize = false;
        [Description("（ZoomFlag为真时）缩放子控件尺寸"), Category("BHXJZoom")]
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件尺寸
        /// </summary>
        public bool ZoomSize
        {
            get { return this._ZoomSize; }
            set
            {
                if (value != this._ZoomSize)
                {
                    this._ZoomSize = value;
                    //重新缩放
                    this.BhxjPanelClient_SizeChanged(this, null);
                }
            }
        }

        private bool _ZoomFont = false;
        [Description("（ZoomFlag为真时）缩放子控件字体"), Category("BHXJZoom")]
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件字体
        /// </summary>
        public bool ZoomFont
        {
            get { return this._ZoomFont; }
            set
            {
                if (value != this._ZoomFont)
                {
                    this._ZoomFont = value;
                    //重新缩放
                    this.BhxjPanelClient_SizeChanged(this, null);
                }
            }
        }

        private bool _ZoomLocation = false;
        [Description("（ZoomFlag为真时）缩放子控件位置"), Category("BHXJZoom")]
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件位置
        /// </summary>
        public bool ZoomLocation
        {
            get { return this._ZoomLocation; }
            set
            {
                if (value != this._ZoomLocation)
                {
                    this._ZoomLocation = value;
                    //重新缩放
                    this.BhxjPanelClient_SizeChanged(this, null);
                }
            }
        }
        #endregion

        /// <summary>
        /// 内部尺寸
        /// </summary>
        [Browsable(false)]
        public Size InnerSize
        {
            get;
            set;
        }

        #region 边框
        /// <summary>
        /// _有边框
        /// </summary>
        private bool _HasFrame = false;
        [Description("有边框"), Category("BHXJ")]
        /// <summary>
        /// 有边框
        /// </summary>
        public bool HasFrame
        {
            get { return this._HasFrame; }
            set
            {
                if (this._HasFrame != value)
                {
                    this._HasFrame = value;
                    this.Invalidate(false);
                }
            }
        }

        /// <summary>
        /// _边框颜色
        /// </summary>
        private Color _FrameColor = Color.FromArgb(64, 64, 64);
        [Description("边框颜色"), Category("BHXJ")]
        /// <summary>
        /// 边框颜色
        /// </summary>
        public Color FrameColor
        {
            get
            {
                return this._FrameColor;
            }
            set
            {
                if (this._FrameColor != value)
                {
                    this._FrameColor = value;
                    this.Invalidate(false);
                }
            }
        }
        #endregion

        #region 按钮
        [Description("按钮颜色"), Category("BHXJ")]
        /// <summary>
        /// 按钮颜色
        /// </summary>
        public Color ControlButtonColor
        {
            get { return this.bhxjButtonClose.BackColor; }
            set
            {
                if (this.bhxjButtonClose.BackColor != value)
                {
                    this.bhxjButtonClose.BackColor = value;
                    this.bhxjButtonMax.BackColor = value;
                    this.bhxjButtonMin.BackColor = value;
                }
            }
        }
        [Description("按钮字体颜色"), Category("BHXJ")]
        /// <summary>
        /// 按钮字体颜色
        /// </summary>
        public Color ControlFontColor
        {
            get { return this.bhxjButtonClose.ForeColor; }
            set
            {
                if (this.bhxjButtonClose.ForeColor != value)
                {
                    this.bhxjButtonClose.ForeColor = value;
                    this.bhxjButtonMax.ForeColor = value;
                    this.bhxjButtonMin.ForeColor = value;
                }
            }
        }
        [Description("鼠标进入按钮颜色"), Category("BHXJ")]
        /// <summary>
        /// 鼠标进入按钮颜色
        /// </summary>
        public Color ButtonEnterColor
        {
            get { return this.bhxjButtonClose.MouseEnterColor; }
            set
            {
                if (this.bhxjButtonClose.MouseEnterColor != value)
                {
                    this.bhxjButtonClose.MouseEnterColor = value;
                    this.bhxjButtonMax.MouseEnterColor = value;
                    this.bhxjButtonMin.MouseEnterColor = value;
                }
            }
        }
        [Description("按钮按下颜色"), Category("BHXJ")]
        /// <summary>
        /// 按钮按下颜色
        /// </summary>
        public Color ButtonDownColor
        {
            get { return this.bhxjButtonClose.MouseDownColor; }
            set
            {
                if (this.bhxjButtonClose.MouseDownColor != value)
                {
                    this.bhxjButtonClose.MouseDownColor = value;
                    this.bhxjButtonMax.MouseDownColor = value;
                    this.bhxjButtonMin.MouseDownColor = value;
                }
            }
        }
        #endregion    

        #region 标题颜色
        //private SolidBrush titlebrush = new SolidBrush(Color.Transparent);
        //[Description("标题颜色"), Category("BHXJ")]
        ///// <summary>
        ///// 标题颜色
        ///// </summary>
        //public Color TitleColor
        //{
        //    get 
        //    {
        //        return this.titlebrush.Color;
        //    }
        //    set
        //    {
        //        if (this.titlebrush.Color != value)
        //        {
        //            this.titlebrush.Color = value;
        //            this.Invalidate(false);
        //        }
        //    }
        //}

        private Color _TitleStartColor = Color.FromArgb(52, 106, 159);
        [Description("标题起始颜色"), Category("BHXJ")]
        /// <summary>
        /// 标题起始颜色
        /// </summary>
        public Color TitleStartColor
        {
            get { return this._TitleStartColor; }
            set
            {
                if (this._TitleStartColor != value)
                {
                    this._TitleStartColor = value;
                    this.Invalidate(false);
                }
            }
        }
        private Color _TitleStopColor = Color.FromArgb(73, 124, 174);
        [Description("标题结束颜色"), Category("BHXJ")]
        /// <summary>
        /// 标题结束颜色
        /// </summary>
        public Color TitleStopColor
        {
            get { return this._TitleStopColor; }
            set
            {
                if (this._TitleStopColor != value)
                {
                    this._TitleStopColor = value;
                    this.Invalidate(false);
                }
            }
        }

        #endregion


        /// <summary>
        /// 界面状态
        /// </summary>
        public new FormWindowState WindowState
        {
            get { return base.WindowState; }
            set
            {
                base.WindowState = value;
                this.WindowStateChanged(this, null);
            }
        }
        /// <summary>
        /// 界面状态变化事件
        /// </summary>
        public EventHandler WindowStateChanged;

        public new bool MinimizeBox 
        {
            get { return base.MinimizeBox; }
            set
            {
                base.MinimizeBox = value;
                this.bhxjButtonMin.Visible = base.MinimizeBox;
            }
        }

        public new bool MaximizeBox
        {
            get { return base.MaximizeBox; }
            set 
            {
                base.MaximizeBox = value;
                this.bhxjButtonMax.Visible = value;
            }
        }


        /// <summary>
        /// 鼠标位置
        /// </summary>
        private Point mousePoint = new Point();
        /// <summary>
        /// 缓存尺寸
        /// </summary>
        private Size cachesize = new Size();
        /// <summary>
        /// 缓存位置
        /// </summary>
        private Point cacheposition = new Point();

        /// <summary>
        /// 鼠标左键已经按住
        /// </summary>
        private bool MouseLeftIsDown = false;

        /// <summary>
        /// 标题Blend
        /// </summary>
        private Blend TitleBlend =GetBlent();

        public BHXJFormN()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            //this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.ResizeRedraw, false);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            //this.SetStyle(ControlStyles.Opaque, true);



            #region 设计时,变化事件
            if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
            {
                //设计时事件
                this.SizeChanged += new EventHandler(this.BHXJ_SizeChangedInDesignMode);
                this.FontChanged += new EventHandler(this.BHXJ_FontChangedInDesignMode);
                this.LocationChanged += new EventHandler(this.BHXJ_LocationChangedInDesignMode);

                //innerSize类
                //this.SizeChanged += new EventHandler(this.SizeChangedInner);
                this.bhxjPanelClient.SizeChanged += new EventHandler(this.SizeChangedInner);

            }
            #endregion

            #region 设置初始值BHXJ
            //设置初始
            this.IniSize = this.Size;
            this.InnerSize = this.bhxjPanelClient.Size;
            this.IniFont = this.Font;
            this.IniLocation = this.BHXJGetIniLocationInDesignMode();
            #endregion

            //防止字体在设计器中添加该控件时候自动追随父控件
            this.Font = this.Font;
            //缩放模式设置为无
            this.AutoScaleMode = AutoScaleMode.None;

            #region 位移
            this.MouseDown += this.B_MouseDown;
            this.MouseMove += this.B_MouseMove;
            this.MouseUp += this.B_MouseUp;


            this.LabelTitle.MouseMove += this.B_MouseMove;
            this.LabelTitle.MouseUp += this.B_MouseUp;
            this.LabelTitle.MouseDown += this.B_MouseDown;
            

            #endregion

            #region 尺寸
            this.SizeChanged += this.Form_SizeChanged;
            this.bhxjPanelClient.SizeChanged += BhxjPanelClient_SizeChanged;
            #endregion

            #region 标题
            this.LabelTitle.Font = this.Font;
            this.LabelTitle.ForeColor = this.ForeColor;
            this.LabelTitle.Text = this.Text;
            this.FontChanged += new EventHandler(BHXJForm_FontChanged);
            this.ForeColorChanged += new EventHandler(BHXJForm_ForeColorChanged);
            this.TextChanged += new EventHandler(BHXJForm_TextChanged);

            //双击
            this.MouseDoubleClick += this.panelTitle_MouseDoubleClick;
            this.LabelTitle.MouseDoubleClick += this.panelTitle_MouseDoubleClick;
            #endregion

            #region 状态变化
            this.WindowStateChanged += WindowState_Changed;
            this.WindowStateChanged(this, null);
            #endregion


            //this.bhxjButtonClose.Location = new Point(this.Width-28,this.bhxjButtonClose.Height);
            //this.bhxjButtonMax.Location = new Point(this.Width - 48, this.bhxjButtonClose.Height);
            //this.bhxjButtonMin.Location = new Point(this.Width - 68, this.bhxjButtonClose.Height);
            //this.LabelTitle.Location = new Point(8,7);

        }
        /// <summary>
        /// 内部尺寸变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SizeChangedInner(object sender, EventArgs e)
        {
            if (this.bhxjPanelClient.DesignMode)
            {
                this.InnerSize = this.bhxjPanelClient.Size;
                this.bhxjButtonMin.Location = new Point(this.Width - 68, 3);
                this.bhxjButtonMax.Location = new Point(this.Width - 48, 3);
                this.bhxjButtonClose.Location = new Point(this.Width - 28, 3);
            }
        }
        /// <summary>
        /// 客户区尺寸变化处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BhxjPanelClient_SizeChanged(object sender, EventArgs e)
        {
            if (this.DesignMode)
                return;

            bool zoomloaction = this._ZoomLocation;
            bool zoomfont = this._ZoomFont;
            bool zoomsize = this._ZoomSize;
            if (!this._ZoomFlag)
            {
                zoomloaction = false;
                zoomfont = false;
                zoomsize = false;
            }

            if ((this.WindowState == FormWindowState.Minimized))
                return;
            Point p = new Point(0, 0);
            float scale = 1;
            if (this._ZoomFlag)
            {
                float inerwith = this.InnerSize.Width;
                float inerhigh = this.InnerSize.Height;


                float Width = this.bhxjPanelClient.Width;
                float Height = this.bhxjPanelClient.Height;


                scale = ((Width) / inerwith > (Height) / inerhigh) ? (Height) / inerhigh : (Width) / inerwith;
                int x = (int)((Width - scale * inerwith) / 2f);
                int y = (int)((Height - scale * inerhigh) / 2f);
                p = new Point(x, y);
            }
            if (!(scale > 0))
                return;
            //重置控件原点，并按照比例放大控件
            foreach (Control con in this.bhxjPanelClient.Controls)
            {
                if (con is IBHXJZOOM)
                {
                    if (con.Dock == DockStyle.None)
                    {

                        IBHXJZOOM zoom = con as IBHXJZOOM;
                        zoom.OriginPoint = p;
                        if (zoomloaction)
                            zoom.BHXJLocationZoom(scale);
                        else
                            zoom.BHXJLocationZoom(1);
                        if (zoomsize)
                            zoom.BHXJSizeZoom(scale);
                        else
                            zoom.BHXJSizeZoom(1);
                        if (zoomfont)
                            zoom.BHXJFontZoom(scale);
                        else
                            zoom.BHXJFontZoom(1);
                    }
                    //else if(con is MenuStrip)
                    //{
                    //    IBHXJZOOM zoom = con as IBHXJZOOM;
                    //    if (zoomfont)
                    //        zoom.BHXJFontZoom(scale);
                    //    else
                    //        zoom.BHXJFontZoom(1);
                    //}
                }
            }
        }
        /// <summary>
        /// form尺寸变化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_SizeChanged(object sender, EventArgs e)
        {
            this.Invalidate(false);
        }

        #region 尺寸拖动

        private void WmNcHitTest(ref Message m)
        {
            int wparam = m.LParam.ToInt32();
            byte[] buff= BitConverter.GetBytes(wparam);
            int x = BitConverter.ToInt16(buff, 0);
            int y = BitConverter.ToInt16(buff, 2);

            //Point mouseLocation = new Point(wparam & 0xFFFF, wparam >> 16);
            Point mouseLocation = new Point(x, y);
            mouseLocation = PointToClient(mouseLocation);

            if (WindowState != FormWindowState.Maximized)
            {
                if (this.AutoSizeMode == AutoSizeMode.GrowOnly)
                {
                    if (mouseLocation.X < 5 && mouseLocation.Y < 5)
                    {
                        m.Result = new IntPtr(Win32.HTTOPLEFT);
                        return;
                    }

                    if (mouseLocation.X > Width - 5 && mouseLocation.Y < 5)
                    {
                        m.Result = new IntPtr(Win32.HTTOPRIGHT);
                        return;
                    }

                    if (mouseLocation.X < 5 && mouseLocation.Y > Height - 5)
                    {
                        m.Result = new IntPtr(Win32.HTBOTTOMLEFT);
                        return;
                    }

                    if (mouseLocation.X > Width - 5 && mouseLocation.Y > Height - 5)
                    {
                        m.Result = new IntPtr(Win32.HTBOTTOMRIGHT);
                        return;
                    }

                    if (mouseLocation.Y < 3)
                    {
                        m.Result = new IntPtr(Win32.HTTOP);
                        return;
                    }

                    if (mouseLocation.Y > Height - 3)
                    {
                        m.Result = new IntPtr(Win32.HTBOTTOM);
                        return;
                    }

                    if (mouseLocation.X < 3)
                    {
                        m.Result = new IntPtr(Win32.HTLEFT);
                        return;
                    }

                    if (mouseLocation.X > Width - 3)
                    {
                        m.Result = new IntPtr(Win32.HTRIGHT);
                        return;
                    }
                }
            }
            //m.Result = new IntPtr(Win32.HTCAPTION);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            //if (this.AutoSizeMode != AutoSizeMode.GrowOnly)
            //{
            //    //禁止标题栏双击事件
            //    if (m.Msg == 0xA3)
            //        return;
            //}
            switch (m.Msg)
            {
                case Win32.WM_NCHITTEST:
                    WmNcHitTest(ref m);
                    break;
            }
        }
        /// <summary>
        /// 状态栏点击生效
        /// </summary>
        protected override CreateParams CreateParams
        {
            get
            {
                const int WS_MINIMIZEBOX = 0x00020000;  // Winuser.h中定义  
                CreateParams cp = base.CreateParams;
                cp.Style = cp.Style | WS_MINIMIZEBOX;   // 允许最小化操作  
                //cp.ExStyle |= (int)0x02000000;
                return cp;
            }
        }
        #endregion

        /// <summary>
        /// 双击放大还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void panelTitle_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.AutoSizeMode != AutoSizeMode.GrowOnly)
                return;
            if (this.WindowState != FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
            }
            this.MouseLeftIsDown = false;
        }

        /// <summary>
        /// 状态变化事件触发函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WindowState_Changed(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.bhxjButtonMax.ButtonType = BHXJButton.BHXJButtonType.放大;
            }
            else if (this.WindowState == FormWindowState.Maximized)
            {
                this.bhxjButtonMax.ButtonType = BHXJButton.BHXJButtonType.还原;
            }
        }
        /// <summary>
        /// 文本变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BHXJForm_TextChanged(object sender, EventArgs e)
        {
            this.LabelTitle.Text = this.Text;
        }
        /// <summary>
        /// 字体变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BHXJForm_FontChanged(object sender, EventArgs e)
        {
            this.LabelTitle.Font = this.Font;
        }
        /// <summary>
        /// 字体颜色变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BHXJForm_ForeColorChanged(object sender, EventArgs e)
        {
            this.LabelTitle.ForeColor = this.ForeColor;
        }

       

        /// <summary>
        /// 重绘
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;
            #region Title
            {
                FillLineGradient(e.Graphics, new Rectangle(0, 0, this.Width - 1, this.Padding.Top-1), this.TitleStartColor, this.TitleStopColor, 90f, TitleBlend);
            }
            #endregion

            #region 边框
            if (this._HasFrame)
            {
                Pen edge = new Pen(this._FrameColor);
                g.DrawLine(edge, 0, 0, this.Width - 1, 0);
                g.DrawLine(edge, 0, 0, 0, this.Height - 1);
                g.DrawLine(edge, 0, this.Height - 1, this.Width - 1, this.Height - 1);
                g.DrawLine(edge, this.Width - 1, 0, this.Width - 1, this.Height - 1);
                //g.DrawLine(edge, 0, this.panelTop.Height + this.panelTitle.Height - 1, this.Width - 1, this.panelTop.Height + this.panelTitle.Height - 1);
                edge.Dispose();
            }
            #endregion
        }

        /// <summary>
        /// 获取blend
        /// </summary>
        /// <returns></returns>
        private static Blend GetBlent()
        {
            Blend result = new Blend();
            float[] fs = new float[5] { 0f, 0.3f, 0.5f, 0.8f, 1f };
            float[] f = new float[5] { 0f, 0.5f, 1f, 0.5f, 0f };
            result.Positions = fs;
            result.Factors = f;
            return result;
        }

        /// <summary>
        /// 填充线性渐变
        /// </summary>
        /// <param name="g">画布</param>
        /// <param name="rect">填充区域</param>
        /// <param name="startcolor">开始颜色</param>
        /// <param name="endcolor">结束颜色</param>
        /// <param name="angle">角度</param>
        /// <param name="blend">对象的混合图案</param>
        private void FillLineGradient(Graphics g, Rectangle rect, Color startcolor, Color endcolor, float angle, Blend blend)
        {
            LinearGradientBrush linebrush = new LinearGradientBrush(rect, startcolor, endcolor, angle);
            if (blend != null)
            {
                linebrush.Blend = blend;
            }
            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(rect);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.FillPath(linebrush, path);
            g.SmoothingMode = SmoothingMode.None;
        }

       

        #region 缩放和关闭按钮事件
        protected virtual void bhxjButtonMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        protected virtual void bhxjButtonMax_Click(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
            }
        }

        protected virtual void bhxjButtonClose_Click(object sender, EventArgs e)
        {
            DialogResult response = MessageBox.Show("您确定要退出该程序么？", "请确认", MessageBoxButtons.YesNo);
            if (response == DialogResult.Yes)
            {
                this.Close();
            }
        }
        #endregion

        #region 位移
        private void B_MouseMove(object sender, MouseEventArgs e)
        {
            //监测左键是否点中预定控件
            if (!this.MouseLeftIsDown)
                return;
            if (e.Button == MouseButtons.Left)
            {
                if (this.WindowState == FormWindowState.Normal)
                {
                    int x = Control.MousePosition.X;
                    int y = Control.MousePosition.Y;
                    //this.Top = Control.MousePosition.Y - mousePoint.Y;
                    //this.Left = Control.MousePosition.X - mousePoint.X;

                    this.Top = this.cacheposition.Y + y - mousePoint.Y;
                    this.Left = this.cacheposition.X + x - mousePoint.X;
                }
            }
        }

        private void B_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                //this.mousePoint.X = e.X;
                //this.mousePoint.Y = e.Y;
                if (e.Button == MouseButtons.Left)
                {
                    this.mousePoint = Control.MousePosition;
                    this.cachesize = this.Size;
                    this.cacheposition.X = this.Left;
                    this.cacheposition.Y = this.Top;
                    this.MouseLeftIsDown = true;
                }
            }
        }

        private void B_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                //this.mousePoint.X = e.X;
                //this.mousePoint.Y = e.Y;
                if (e.Button == MouseButtons.Left)
                {
                    this.MouseLeftIsDown = false;
                }
            }
        }
        #endregion

    }
}
